<?php
class Pages extends MY_Controller {

	var $currentPage = "pages";
	var $caption = "Page";	
	
	public function index() {
		
		
		$data['pageList'] = $this->mPage->getAllEntries();
		
		$this->loadAdminPage($data );
	}

	public function create($data='') {
		$this->action = 'add';
		
		$data = $this->CKEditor();
		
		$this->loadAdminPage($data);
	}
	
	public function add() {
		$page = &$this->mPage;
		
		$this->setValues($page);
		
		$this->message = INSERT_ERROR;
		
		if(strlen($page->name) != 0 ) {
			if(count($this->mPage->find_by_name($page->name))==0) {
				$this->message = INSERT_SUCCESS;
				$page->insert();
			} else {
				$this->message = HAS_DUPLICATE . " name.";
				$data['pageData'] = $page;
				$this->create($data);
				return;
			}
		} else {
			$data['page'] = $page;
			$this->create($data);
			return;
		}
		
		$this->index();
	}
	
	public function edit($id) {
		$page = $this->mPage->getEntryById($id);
		
		$data = $this->CKEditor();
		
		if(count($page) == 0) {
			show_error("Invalid $caption", 500, "Invalid data");
		}
	
		$data['pageList'] = $this->mPage->getAllEntries();
		
		$data['pageData'] = $page[0];
		
		$this->action = 'edit';
		$this->id = $page[0]->id;
		$this->loadAdminPage($data);		
	}
	
	public function update() {
		
		
		$page = &$this->mPage;
		
		$this->setValues($page);
		
		//echo $page->content;
		
		$this->message = UPDATE_ERROR;
	
		if(strlen($page->id) != 0) {
		
			if($page->update())
				$this->message = UPDATE_SUCCESS;
			
		}
		
		$this->edit($page->id);
	}
	
	public function delete($id) {
		$this->message = DELETE_ERROR;
		
		if($this->mPage->delete($id))
			$this->message = DELETE_SUCCESS;
		
		$this->index();		
	}
	
	function setValues($page){
		$page->id = $this->input->post('id',TRUE);
		$page->name = trim($this->input->post('name',TRUE));
		//$page->php_file = trim($this->input->post('php_file',TRUE));
		$page->title = trim($this->input->post('title',TRUE));
		$page->content = htmlspecialchars($_REQUEST['content']);
		//$page->parent_id = $this->input->post('parent_id',TRUE);
	}
}
?>